package player.mplayer;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;




public class MediaManager {
	static String mplayerPath=".\\mplayer\\mplayer.exe";
	static HashMap<String, MediaInfo> mediaInfoTable=new HashMap<String, MediaInfo>() ;
	static Thread mainThread;
	
	static HashMap<String, MediaInfo> infoTable=new HashMap<String, MediaInfo>();

	static HashSet<String> haveGetInfoTable=new HashSet<String>();
	public static void init(){
		
		mainThread=new Thread(){
			public void run(){
				while(true){
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};
		mainThread.start();
	}
	
	public static MediaInfo creatMediaInfo2(String filePath){
		if(!containsMediaInfo(filePath)){
			MediaInfo mf=new MediaInfo();
			File f=new File(filePath);
			if(f.exists()){
				String s1=f.getName();
				int index=s1.lastIndexOf(".");
				String s2=s1;
				String s3="";
				if(index>0){
					s2=s1.substring(0, index);
					s3=s1.substring(index+1);
				}
				mf.fileName=s1;
				mf.fileSize=f.length();
				mf.length=0;
				mf.width=0;
				mf.height=0;
				mf.fileType=s3;
				mf.mediaPath=filePath;
			}
			mediaInfoTable.put(filePath, mf);
		}
		return (MediaInfo) mediaInfoTable.get(filePath);
	}
	
	static boolean containsMediaInfo(String filePath){
		if(mediaInfoTable==null){
			mediaInfoTable=new HashMap();
		}
		return mediaInfoTable.containsKey(filePath);
	}
	
	
	public static MediaInfo getMediaInfo(String filePath){
		File file=new File(filePath);
		String key=file.getAbsolutePath();
		if(!mediaInfoTable.containsKey(key)){
			MediaInfo mi=creatMediaInfo2(file.getAbsolutePath());
			mediaInfoTable.put(key, mi);
			if(!haveGetInfoTable.contains(key)){
				 createMediaInfoFromMPlayer(file,mi);
				 haveGetInfoTable.add(file.getAbsolutePath());
			}
		}else{
			
		}
		
		//createMediaInfo(file);
		MediaInfo info=infoTable.get(file.getAbsolutePath());
		MediaInfo info2=new MediaInfo();
		info2.mediaPath=info.mediaPath;
		info2.fileName=info.fileName;
		return  info2;
	}

	synchronized static void createMediaInfoFromMPlayer(File file,final MediaInfo info){

		Process proc ;

		String[] cmd = new String[] {
				mplayerPath,
				"-identify", 
				"-nosound", 
				"-vc","dummy",
				"-vo","null",
				"-ss",String.valueOf(0),
				file.getAbsolutePath()};
		try {
			proc = Runtime.getRuntime().exec(cmd);
		} catch (IOException e1) {
			e1.printStackTrace();
			return;
		}

		final InputStream is1 = proc.getErrorStream();
		final InputStream is2 = proc.getInputStream();
		final Runnable errorReader = new Runnable() {
			public void run() {
				try {
					final BufferedReader lReader = new BufferedReader(new InputStreamReader(is1));
					for (String l = lReader.readLine(); l != null ; l = lReader.readLine()) {
						// System.out.println("ERROR "+l);
					}
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		};


		final Runnable standReader = new Runnable() {
			public void run() {
				try {
					final BufferedReader lReader = new BufferedReader(
							new InputStreamReader(is2));
					for (String l = lReader.readLine(); l != null; l = lReader.readLine()) {
							//System.out.println("OUT "+l);
						if (l.length() >= 10) {
							String s2 = l.substring(0, 9);
							if (s2.equals("ID_LENGTH")) {
								int index = l.indexOf(".");
								String s1 = l.substring(10,index);
								info.length = Integer.valueOf(s1);
							}
							if (l.startsWith("ID_VIDEO_FORMAT")) {
								int index = l.indexOf("=");
								if (index > 0) {
									String value = l.substring(index + 1);
									info.format=value;
								}
							}
							if (l.startsWith("ID_VIDEO_WIDTH")) {
								int index = l.indexOf("=");
								String value = l.substring(index + 1);
								info.width = Integer.valueOf(value);
							}
							if (l.startsWith("ID_VIDEO_HEIGHT")) {
								int index = l.indexOf("=");
								String value = l.substring(index + 1);
								info.height = Integer.valueOf(value);
							}
							if (l.startsWith("ID_VIDEO_FPS")) {
								int index = l.indexOf(".");
								int index1 = l.indexOf("=")+1;
								String s1 = l.substring(index1,
										index);
								info.fps = Integer.valueOf(s1);
							}
							if(l.startsWith("ANS_length=")){
								int t=	Float.valueOf(s2).intValue();
								int length=t;
								info.length=length;
							}
						}
						if(l.length()>70&&l.startsWith("V:")){
							int index = l.indexOf(".");
							String s3 = l.substring(2, index);
							int index2 = 0;
							while (true) {
								int index3 = s3.indexOf(" ", index2);
								if (index3 != -1) {
									index2 = index3 + 1;
								} else {
									break;
								}
							}
						}

					}
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		};

		Thread t1 = new Thread(errorReader);
		Thread t2 = new Thread(standReader);
		t1.start();
		t2.start();











		info.fileName=file.getName();
		info.fileDir=file.getParent();
		info.filePath=file.getAbsolutePath();
		info.size=file.length();
		infoTable.put(file.getAbsolutePath(), info);
		try {
			proc.waitFor();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	
}
